package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    private StudentDao studentDao = new StudentDao();

    public boolean addStudent(Student stu) {
        // 1. 创建StudentDao(库管)
        // 2. 将学生对象，传递给StudengDao库管中的addStudent方法
        // 3. 将返回的boolean类型结果，返还给StudentController
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
        Student[] stus =  studentDao.findAllStudent();
        boolean exists = false;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if(student != null && student.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Student[] findAllStudent() {
        Student[] allStudent = studentDao.findAllStudent();
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            if(stu != null){
                flag = true;
                break;
            }
        }
        if(flag){
            return allStudent;
        }else{
            return null;
        }
    }

    public void deleteStudentById(String id) {
        studentDao.deleteStudentById(id);
    }


    public void updateStudent(String updId, Student stu) {
        studentDao.updateStudent(updId,stu);
    }
}
